package smecta.client.print.controller;

import smecta.client.print.util.RowData;

import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;

public class StckListReportViewController {

	@FXML
	private DatePicker startDatePicker;

	@FXML
	private DatePicker endDatePicker;

	@FXML
	private ComboBox<String> shiftComboBox;

	@FXML
	private ComboBox<String> machineComboBox;

	@FXML
	private TextField productNameField;

	@FXML
	private ComboBox<String> specComboBox;

	@FXML
	private ComboBox<String> colorComboBox;

	@FXML
	private TextField batchNumberField;

	@FXML
	private ComboBox<String> gradeComboBox;

	@FXML
	private TextField orderNumberField;

	@FXML
	private TableView<?> dataTableView;

	@FXML
	private TableColumn<?, ?> colOrderNumber;

	@FXML
	private TableColumn<?, ?> colProductName;

	@FXML
	private TableColumn<?, ?> colSpec;

	@FXML
	private TableColumn<?, ?> colBatchNumber;

	@FXML
	private TableColumn<?, ?> colColor;

	@FXML
	private TableColumn<?, ?> colGrade;

	@FXML
	private TableColumn<?, ?> colFixedWeight;

	@FXML
	private TableColumn<?, ?> colPackageCount;

	@FXML
	private TableColumn<?, ?> colNetWeight;

	@FXML
	private TableColumn<?, ?> colSelection;

	@FXML
	private Label totalOrderCount;

	@FXML
	private Label totalProductCount;

	@FXML
	private Label totalNetWeight;

	@FXML
	private HBox summaryHBox;

	@FXML
	private void initialize() {
		// Bind the width of the summary labels to the width of the table columns
		bindSummaryColumnWidths();
	}

	@FXML
	private void handleSearch() {
		// 查询逻辑
		// 在查询逻辑中更新汇总信息
		updateSummary();
	}

	@FXML
	private void handleExport() {
		// 导出逻辑
	}

	@FXML
	private void handlePreview() {
		// 预览逻辑
	}

	@FXML
	private void handlePrint() {
		// 打印逻辑
	}

	@FXML
	private void handleVoid() {
		// 作废逻辑
	}

	@FXML
	private void handleEngrave() {
		// 刻码逻辑
	}

	@FXML
	private void handleViewFiles() {
		// 查看文件逻辑
	}

	private void updateSummary() {
		// 计算并更新汇总信息
		int totalOrders = dataTableView.getItems().size();
//		double totalWeight = dataTableView.getItems().stream().mapToDouble(item -> ((RowData) item).getNetWeight()) // 假设YourDataType有getNetWeight方法
//				.sum();
//
//		totalOrderCount.setText(String.valueOf(totalOrders));
//		totalNetWeight.setText(String.format("%.2f", totalWeight));
//		// 这里假设每个品名是唯一的，如果不是，需要调整计算逻辑
//		long totalProducts = dataTableView.getItems().stream().map(item -> ((YourDataType) item).getProductName())
//				.distinct().count();
//
//		totalProductCount.setText(String.valueOf(totalProducts));
	}

	private void bindSummaryColumnWidths() {
		// Bind the summary labels' widths to the corresponding table columns' widths
		totalOrderCount.prefWidthProperty().bind(colOrderNumber.widthProperty());
		totalProductCount.prefWidthProperty().bind(colProductName.widthProperty());
		totalNetWeight.prefWidthProperty().bind(colNetWeight.widthProperty());
	}
}
